package com.example.emos.wx.message.db.dao;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.example.emos.wx.message.model.pojo.MessageEntity;
import com.example.emos.wx.message.model.pojo.MessageRefEntity;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 消息模块的DAO层接口
 * @Author YinXi
 * @Versin 1.0.0
 * @Date 2023/9/12
 */
@Repository
public class MessageDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 向mongoDB中插入数据
     */
    public String insert(MessageEntity entity){
        //把北京时间转换为格林时间否则无法插入
        Date sendTime = entity.getSendTime();
        //北京在东8区所以偏移8个小时就是格林时间
        sendTime = DateUtil.offset(sendTime, DateField.HOUR,8);
        entity.setSendTime(sendTime);
        entity = mongoTemplate.save(entity);
        //返回的是mongoDB生成的uuid值为唯一的id标识
        return entity.get_id();
    }
    /**
     * 分页查询消息
     */
    public List<HashMap> searchMessageByPage(int userId,long start,int length){
        //集合连接
        JSONObject json = new JSONObject();
        json.set("$toString","$_id");
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.addFields().addField("id").withValue(json).build(),
                Aggregation.lookup("message_ref","id","messageId","ref"),
                Aggregation.match(Criteria.where("ref.receiverId").is(userId)),
                Aggregation.sort(Sort.by(Sort.Direction.DESC,"sendTime")),
                Aggregation.skip(start),
                Aggregation.limit(length)
        );
        AggregationResults<HashMap> results = mongoTemplate.aggregate(aggregation,"message",HashMap.class);
        List<HashMap> list = results.getMappedResults();
        list.forEach(one->{
            List<MessageRefEntity> refList = (List<MessageRefEntity>) one.get("ref");
            MessageRefEntity entity = refList.get(0);
            boolean readFlag = entity.getReadFlag();
            String refId = entity.get_id();
            one.put("readFlag",readFlag);
            one.put("refId",refId);
            one.remove("ref");
            one.remove("_id");
            Date sendTime = (Date)one.get("sendTime");
            sendTime = DateUtil.offset(sendTime,DateField.HOUR,-8);
            String today = DateUtil.today();
            if(today.equals(DateUtil.date(sendTime).toDateStr())){
                one.put("sendTime",DateUtil.format(sendTime,"HH:mm"));
            }else {
                one.put("sendTime",DateUtil.format(sendTime,"yyyy/MM/dd"));
            }
        });
        return list;
    }
    /**
     * 根据id查找数据
     */
    public HashMap searchMessageById(String id){
        HashMap map = mongoTemplate.findById(id,HashMap.class,"message");
        Date sendTime = (Date)map.get("sendTime");
        sendTime=DateUtil.offset(sendTime,DateField.HOUR,-8);
        map.replace("sendTime",DateUtil.format(sendTime,"yyyy-MM-dd HH:mm"));
        return map;
    }

    public long deleteUserMessage(Integer receiverId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("receiverId").is(receiverId));
        DeleteResult result=mongoTemplate.remove(query,"message");
        long rows = result.getDeletedCount();
        return rows;
    }
}
